﻿#ifndef __SS_BASE_DEFINE_H__
#define __SS_BASE_DEFINE_H__
/*                          _ooOoo_
                           o8888888o
                           88" . "88
                           (| -_- |)
                            O\ = /O
                        ____/`---'\____
                        . ' \\| |// `.
                       / \\||| : |||// \
                     / _||||| -:- |||||- \
                       | | \\\ - /// | |
                     | \_| ''\---/'' | |
                      \ .-\__ `-` ___/-. /
                   ___`. .' /--.--\ `. . __
                ."" '< `.___\_<|>_/___.' >'"".
               | | : `- \`.;`\ _ /`;.`/ - ` : | |
                 \ \ `-. \_ __\ /__ _/ .-` / /
         ======`-.____`-.___\_____/___.-`____.-'======
                            `=---='
         .............................................
                  佛祖保佑            永无BUG */
#include <malloc.h>
#include <new>
//#include <stdint.h>

namespace StarSeeker
{

#ifndef SS_TYPEDEF
#define SS_TYPEDEF
//基础类型
typedef int					i32;
typedef short				i16;
typedef char				i8;
typedef unsigned int		u32;
typedef unsigned short		u16;
typedef unsigned char		u8;
//#if defined(SS_USE_WINDOWS) && _MSC_VER<1400
//typedef __int64				i64;
//typedef unsigned __int64	u64;
//#else //#elif defined SS_USE_ANDROID
typedef long long int		i64;
typedef unsigned long long int	u64;
//#endif
typedef float				f32;
typedef double				f64;
#ifdef SS_USE_32BIT
typedef long				lint;
typedef unsigned long		ulint;
#else
typedef i64					lint;
typedef u64					ulint;
#endif
//自定义类型
typedef int					bool32;
typedef int					wchar;
#ifdef SS_USE_DOUBLE_FLOAT
typedef double				ssfloat;
#else
typedef float				ssfloat;
#endif
typedef short				error_t;
#endif

#ifndef SS_TRUE
#define SS_TRUE 0
#endif

#ifndef SS_FALSE
#define SS_FALSE 1
#endif

#ifndef SS_INVAILD_U32
#define SS_INVAILD_U32 0xffffffff
#endif

#ifndef SS_INVAILD_U64
#define SS_INVAILD_U64 0xffffffffffffffff
#endif

#ifndef SS_INVAILD_UINT
#ifdef SS_USE_64_INT
	#define SS_INVAILD_UINT 0xffffffffffffffff
#else
	#define SS_INVAILD_UINT 0xffffffff
#endif
#endif

#define SS_1KB 1024
#define SS_1MB 1048576
#define SS_1GB 1073741824

#ifndef SS_SAFE_DELETE
#define SS_SAFE_DELETE(p) if(p){delete (p); (p)=0;}
#endif
#ifndef SS_SAFE_DEL_ARRAY
#define SS_SAFE_DEL_ARRAY(p) if(p){delete [] (p); (p)=0;}
#endif
#ifndef SS_SAFE_FREE
#define SS_SAFE_FREE(p) if(p){::free(p); (p)=0;}
#endif
#ifndef SS_SAFE_RELEASE
#define SS_SAFE_RELEASE(p) if(p){(p)->Release(); (p)=0;}
#endif
#ifndef SS_SAFE_NEW
#define SS_SAFE_NEW(p,type) if(!p){(p)=new type;}
#endif
#ifndef SS_SAFE_NEW_ARRAY
#define SS_SAFE_NEW_ARRAY(p,type,num) if(!p && num>0){(p)=new type[num];}
#endif
#ifndef SS_MEMZERO
#define SS_MEMZERO(p,len) if(p && len>0){memset(p,0,len);}
#define SS_MEMZERO_ARRAY(p) if(p){memset(p,0,sizeof(p));}
#endif
#ifndef SS_ARRAY_COUNT
#define SS_ARRAY_COUNT(arr) (sizeof(arr)/sizeof(arr[0]))
#endif
#ifndef SS_MALLOC
#define SS_MALLOC(type,len) ((type*)::malloc(len))
#endif
#ifndef SS_MALLOC_ZERO
#define SS_MALLOC_ZERO(p,len) if(!p && len>0){(p)=::malloc(len); if(p)memset(p,0,len);}
#endif

#define SS_STATIC_CAST(type,val) static_cast<type>(val) //一般的转换(no run-time check),必须是有关联的类型
#define SS_DYNAMIC_CAST(type,val) dynamic_cast<type>(val) //通常在基类和派生类之间转换时使用，会检测类型
#define SS_REINTERPRET_CAST(type,val) reinterpret_cast<type>(val) //用于进行没有任何关联之间的转换(run-time check)，比如一个字符指针转换为一个整形数
#define SS_CONST_CAST(type,val) const_cast<type>(val) //主要针对const和volatile的转换

#define SS_API __stdcall //汇编调用推荐，由函数本身清除栈
#define SS_CALLBACK __stdcall
#define SS_API_C __cdecl //C语言标准，由函数调用者清除栈

const u32 cg_i32_size = sizeof(i32);
const u32 cg_u32_size = sizeof(u32);
const u32 cg_i8_size = sizeof(i8);
const u32 cg_u8_size = sizeof(u8);
const u32 cg_i16_size = sizeof(i16);
const u32 cg_u16_size = sizeof(u16);
const u32 cg_i64_size = sizeof(i64);
const u32 cg_u64_size = sizeof(u64);
const u32 cg_f32_size = sizeof(f32);
const u32 cg_f64_size = sizeof(f64);
const u32 cg_point_size = sizeof(void*);
const u32 cg_lint_size = sizeof(lint);
const u32 cg_ulint_size = sizeof(ulint);
const u32 cg_folat_size = sizeof(ssfloat);
const u32 cg_wchar_size = sizeof(wchar);

union ST32_u
{
	i8 i8p;
	u8 u8p;
	i16 i16p;
	u16 u16p;
	i32 i32p;
	u32 u32p;
	f32 f32p;
#ifdef SS_USE_32BIT
	lint lip;
	ulint ulip;
	void* pp;
#endif
};

union ST64_u
{
	i8 i8p;
	u8 u8p;
	i16 i16p;
	u16 u16p;
	i32 i32p;
	u32 u32p;
	i64 i64p;
	u64 u64p;
	f32 f32p;
	f64 f64p;
	lint lip;
	ulint ulip;
	void* pp;
	ST32_u st32p;
};

inline bool CheckVariableLength()
{
	if (cg_i32_size==4 && cg_u32_size==4 && cg_i8_size==1 && cg_u8_size==1
		&& cg_i16_size==2 && cg_u16_size==2 && cg_i64_size==8 && cg_u64_size==8
		&& cg_f32_size==4 && cg_f64_size==8)
	{
#ifdef SS_USE_32BIT
		if (cg_point_size==4 && cg_lint_size==4 && cg_ulint_size==4 && sizeof(ST32_u)==4
			&& sizeof(ST64_u)==8)
#else
		if (cg_point_size==8 && cg_lint_size==8 && cg_ulint_size==8 && sizeof(ST32_u)==4
			&& sizeof(ST64_u)==8)
#endif
			return true;
	}
	return false;
}

}

#endif
